package com.basic.web.service.impl;

import com.basic.common.constant.CommonDsConstant;
import com.basic.common.exception.business.DataException;
import com.basic.common.result.Errors;
import com.basic.common.result.WebResult;
import com.basic.web.controller.req.SelectByOneForm;
import com.basic.web.dao.mapper.CmsWebContentExMapper;
import com.basic.web.dao.mapper.CmsWebContentMapper;
import com.basic.web.dao.model.CmsWebContent;
import com.basic.web.dao.model.CmsWebContentExample;
import com.basic.web.domain.CmsWebContentBo;
import com.basic.web.service.CmsWebContentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CmsWebContentServiceImpl implements CmsWebContentService {
    @Autowired
    private CmsWebContentMapper cmsWebContentMapper;
    @Autowired
    private CmsWebContentExMapper cmsWebContentExMapper;
    @Override
    public WebResult addCmsWebContent(CmsWebContent cmsWebContent) {
        //查询code值是否存在
        CmsWebContentExample cmsWebContentExample = new CmsWebContentExample();
        CmsWebContentExample.Criteria criteria = cmsWebContentExample.createCriteria();
        criteria.andCodeEqualTo(cmsWebContent.getCode());
        criteria.andIsEnabledEqualTo(CommonDsConstant.IS_ENABLED);
        List<CmsWebContent> cmsWebContents = cmsWebContentMapper.selectByExample(cmsWebContentExample);
        if (!CollectionUtils.isEmpty(cmsWebContents)){
            throw new DataException("CODE值已存在，请更换");
        }
        //设置创建时间
        cmsWebContent.setCreateTime(getNow());
        //设置修改时间
        cmsWebContent.setUpdateTime(getNow());
        //设置版本号
        cmsWebContent.setVno(String.valueOf(System.currentTimeMillis()));
        int i = cmsWebContentMapper.insertSelective(cmsWebContent);
        if (i > 0){
            return WebResult.success();
        }
        return WebResult.fail(Errors.SYSTEM_INSERT_ERROR);
    }

    @Override
    public WebResult updateWebContent(CmsWebContent cmsWebContent) {
        //查询code值是否存在
        CmsWebContentExample updateExample = new CmsWebContentExample();
        CmsWebContentExample.Criteria updateCriteria = updateExample.createCriteria();
        updateCriteria.andCodeEqualTo(cmsWebContent.getCode());
        updateCriteria.andIsEnabledEqualTo(CommonDsConstant.IS_ENABLED);
        List<CmsWebContent> cmsWebContents = cmsWebContentMapper.selectByExample(updateExample);
        if (!CollectionUtils.isEmpty(cmsWebContents)){
            throw new DataException("CODE值已存在，请更换");
        }
        CmsWebContentExample cmsWebContentExample = new CmsWebContentExample();
        CmsWebContentExample.Criteria criteria = cmsWebContentExample.createCriteria();
        criteria.andIdEqualTo(cmsWebContent.getId());
        //设置修改时间
        cmsWebContent.setUpdateTime(getNow());
        //设置版本号
        cmsWebContent.setVno(String.valueOf(System.currentTimeMillis()));
        int i = cmsWebContentMapper.updateByExampleSelective(cmsWebContent, cmsWebContentExample);
        if (i > 0){
            return WebResult.success();
        }
        return WebResult.fail(Errors.SYSTEM_UPDATE_ERROR);

    }

    @Override
    public WebResult deleteWebContent(long id) {
        //进行假删
        CmsWebContentExample cmsWebContentExample = new CmsWebContentExample();
        CmsWebContentExample.Criteria criteria = cmsWebContentExample.createCriteria();
        criteria.andIdEqualTo(id);
        //创建网页内容对象
        CmsWebContent cmsWebContent = new CmsWebContent();
        cmsWebContent.setIsEnabled(CommonDsConstant.IS_NOT_ENABLED_VAL);
        int i = cmsWebContentMapper.updateByExampleSelective(cmsWebContent, cmsWebContentExample);
        //判断是否成功
        if (i > 0){
            return WebResult.success();
        }
        return WebResult.fail(Errors.SYSTEM_DELETE_ERROR);
    }

    @Override
    public List<CmsWebContent> selectAll(CmsWebContent cmsWebContent) {
        CmsWebContentExample cmsWebContentExample = new CmsWebContentExample();
        CmsWebContentExample.Criteria criteria = cmsWebContentExample.createCriteria();
        //查询条件
        //判断是否已查询时间为条件
        if (cmsWebContent.getCreateTime() != null){
            criteria.andCreateTimeEqualTo(cmsWebContent.getCreateTime());
        }
        //判断是否已修改时间为条件
        if (cmsWebContent.getUpdateTime() != null){
            criteria.andUpdateTimeEqualTo(cmsWebContent.getUpdateTime());
        }
        //判断是否已类型为查询条件
        if (StringUtils.isNotBlank(cmsWebContent.getType())){
            criteria.andTypeEqualTo(cmsWebContent.getType());
        }
        //判断是否已名称为条件
        if (StringUtils.isNotBlank(cmsWebContent.getName())){
           criteria.andNameLike("%" +cmsWebContent.getName() +"%");
        }
        //判断是否已编辑人为条件
        if (cmsWebContent.getBoId() != null){
            criteria.andBoIdEqualTo(cmsWebContent.getBoId());
        }
            criteria.andIsEnabledEqualTo(CommonDsConstant.IS_ENABLED);
        List<CmsWebContent> cmsWebContents = cmsWebContentMapper.selectByExample(cmsWebContentExample);
        if (CollectionUtils.isEmpty(cmsWebContents)){
            cmsWebContents = new ArrayList<>();
        }
        return cmsWebContents;
    }

    @Override
    public WebResult selectAllPage(CmsWebContentBo cmsWebContentBo) {
        //判断页码与页数是否规范
        if (cmsWebContentBo.getPage() == 0 || cmsWebContentBo.getPageSize() == 0){
            cmsWebContentBo.setPage(CommonDsConstant.PAGE_NUM);
            cmsWebContentBo.setPageSize(CommonDsConstant.PAGE_SIZE);
        }
        //复制参数
        CmsWebContent cmsWebContent = new CmsWebContent();
        BeanUtils.copyProperties(cmsWebContentBo,cmsWebContent);
        //使用PageInfo
        PageHelper.startPage(cmsWebContentBo.getPage(),cmsWebContentBo.getPageSize());
        List<CmsWebContent> cmsWebContents = this.selectAll(cmsWebContent);
        PageInfo<CmsWebContent> pageInfo = new PageInfo<>(cmsWebContents);
        return WebResult.success(pageInfo);
    }

    /***
     * 查询单个数据
     * @param selectByOneForm 查询条件
     * @return
     */
    @Override
    public WebResult SelectByOne(SelectByOneForm selectByOneForm) {
        CmsWebContent cmsWebContent = cmsWebContentExMapper.selectByOne(selectByOneForm);
        if (cmsWebContent == null){
            throw new DataException("数据不存在");
        }
        return WebResult.success(cmsWebContent);
    }

    //获取当前时间(去除毫秒)
    public Date getNow(){
        //设置创建时间
        Date now = new Date();
        // 将毫秒部分清零
        now.setTime(now.getTime() / 1000 * 1000);
        return now;
    }
}
